package com.gxpt.app.wxapi;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.gxpt.app.view.Activity_pay_success;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.utils.T;
import com.utils.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{

	private IWXAPI api;

	Context context;

	String orderID="";
	int    type =0 ;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		EventBus.getDefault().register(this);
		setContentView(R.layout.pay_result);
		context = this;
		api = WXAPIFactory.createWXAPI(this, Constant.APP_ID);

		api.handleIntent(getIntent(), this);
		Logger.i("1111");
		EventBus.getDefault().post(new MessageEvent(Constant.ACTION_order_info));
		finish();
//		Intent gonew = new Intent(context,WXPayEntryActivity.class);
//		startActivity(gonew);

	}

//	@Override
//	protected void onDestroy() {
//		EventBus.getDefault().unregister(this);
//		super.onDestroy();
//	}
//
//	@Subscribe(threadMode = ThreadMode.MAIN)
//	public void onMoonEvent(MessageEvent messageEvent){
//		if(messageEvent.getMessageType()==Constant.ACTION_order_info){
//			orderID = messageEvent.getMessage();
//			type    = messageEvent.getIndex();
//
//			Logger.i("orderID = "+orderID);
//		}
//	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
		Logger.i("2222");
	}
	@Override
	public void onResp(BaseResp resp) {
		Logger.i("onPayFinish, errCode = " + resp.toString());

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			// 支付

			int errCode = resp.errCode;
			if (errCode == -1) {
				T.show(context,resp.errStr, 1);
				finish();
			} else if (errCode == 0) {
//				T.show(context, "支付完成", 1);
				EventBus.getDefault().post(new MessageEvent(Constant.ACTION_pay_success));
				Intent goSuccess = new Intent();
				goSuccess.setClass(context,Activity_pay_success.class);
				startActivity(goSuccess);
				finish();
			} else {
				T.show(context,  "支付失败", 1);
				finish();
			}
		}

	}


	@Override
	public void onReq(BaseReq baseReq) {

	}
}